export interface ConfigLock {
  value: string;
  timestamp: number;
}

export type Env = { key: string } & (
  | { type: "inherit" }
  | { type: "custom"; custom: string }
);

export interface Config {
  token: string;
  cloudflareId: string;
  default: {
    host: {
      ssh: { server: { allowedKeys: string[] } };
      envs: { key: string; value: string }[];
    };
  };
  scripts: { id: string; script: string }[];
  hosts: {
    id: string;
    token: string;
    name: string;
    restart: number;
    ssh: {
      publicKey: string;
      privateKey: string;
      server: { port: number; allowedKeys: string[]; enabled: boolean };
    };
    envs: Env[];
    crontabs: {
      id: string;
      cron: string;
      script: string;
      envs: Env[];
      enabled: boolean;
    }[];
  }[];
  forwards: {
    id: string;
    from: { host: string } & (
      | { type: "ssh" }
      | { type: "custom"; custom: { ip: string; port: number } }
    );
    to: { host: string; port: number };
  }[];
}

export type Script = Config["scripts"][number];
export type Host = Config["hosts"][number];
export type Crontab = Host["crontabs"][number];
export type Forward = Config["forwards"][number];

export interface Storage {
  timestamp: number;
  os: { name: string; release: string };
  uptime: number;
  ips: string[];
  ssh: { state: "stop"; error: string } | { state: "running"; port: number };
  forwards: Record<
    string,
    | { state: "stop"; error: string }
    | { state: "running"; to: { port: number } }
    | undefined
  >;
  crontabs: Record<
    string,
    { state: "stop"; error: string } | { state: "running" } | undefined
  >;
}
